home *** CD-ROM | disk | FTP | other *** search
- Path: in-news.erinet.com!usenet
- From: blueice@erinet.com (blueice)
- Newsgroups: comp.lang.c
- Subject: Printing a histogram
- Date: Sun, 25 Feb 1996 17:19:30 GMT
- Organization: EriNet Online 513 436-9915
- Message-ID: <4gq5sa$1u8@eri2.erinet.com>
- NNTP-Posting-Host: edlp061.erinet.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- I am new to programming. Currently I am working on a little
- programming problem that involves getting a list of numbers (with a ra
- nge of 0 to 100) from a user and then outputting a histogram which
- will print an asterick next to each range of numbers for every number
- falling in that range.
- For instance if a user enters 11,12,and 51 the histogram will look
- like:
- 00-09:
- 10-19:**
- 20-29:
- 30-39:
- 40-49:
- 50-59:*
- 60-69:
- 70-79:
- 80-89:
- 90-99:
- 100-:
-
- I was able to write up some pseudocode as follows:
- 1) Use a loop to get numbers from the user and store these numbers in
- an array.
- 2) Pass that array to a function which loops through the array and
- uses 11 different IF statements to decide what range (i.e.
- 0-9,10-19,etc.) each number falls in. Each range has a variable
- assigned to it (using letters of the alphabet as variable names:
- a,b,c......up to k) and the value of that variable is incremented by
- one every time a number is found within the respective range. All
- variables should begin with a value of 0 obviously.
- 3) All eleven variables are then passed to another function which
- prints the ranges and an appropriate number of astericks next to each
- range. 11 different FOR loops are used (one for each variable) to
- print the proper number of astericks next to each range.
-
- I bravely coded this mess and it did work with some minor bugs. The
- code itself looked so horrible to me that I am embarrassed to post it.
- The problem is in using so many IF statements and FOR loops, the code
- looks way too messy and inefficient. I would appreciate any advice on
- how to code this problem more intelligently.
- Incidentally I am only learning to program as a hobby because I know
- that someone with my lack of talent and ability (as evidenced by the
- back asswards way I am trying to write this code) could never hope to
- make money at programming. However, I would appreciate it if any of
- you pros could lower yourselves to help the mentally challenged.
- Thanks so much.
-
-
-
- blueice@erinet.com
-
- ***Please don't flame or I will melt!***
-
-